<?php

namespace p5\objects;

/**
 * Created by PhpStorm.
 * User: simon
 * Date: 2015/11/23
 * Time: 10:02
 */
class ValidateUtils
{

    public static function isNotEmpty($object)
    {
        return !self::isEmpty($object);
    }

    public static function isEmpty($object)
    {
        if ($object == null) {
            return true;
        }
        if (is_string($object)) {
            return $object === "";
        }
        if (empty($object)) {
            return true;
        }
        if (is_array($object)) {
            return count($object) == 0;
        }
        return false;
    }

    public static function isMobile($mobile)
    {
        $mobile = trim($mobile);
        if (strlen($mobile) != 11) {
            return false;
        }
        $pattern = '/^(13|14|15|16|17|18|19)(\\d{9})$/i';
        if (preg_match($pattern, $mobile)) {
            // echo "$mobile is mobile" ;
            return true;
        }
        return false;
    }

    public static function isInt($string)
    {
        $pattern = '/^[0-9]\d*$/i';
        if (preg_match($pattern, $string)) {
            return true;
        }
        return false;
    }

    public static function isNum($string)
    {
        $pattern = '/^[0-9]*[.]?[0-9]*$/i';
        if (preg_match($pattern, $string)) {
            return true;
        }
        return false;
    }

    public static function isTimestamp($string)
    {
        //like 1487236500000
        $pattern = '/^(14|15)(\\d{11})$/i';
        if (preg_match($pattern, $string)) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是日期时间格式
     * @param $string
     * @return bool
     */
    public static function isDateTime($string)
    {
        $p1 = "/\d{4}(\-|\.|\/)\d{2}(\-|\.|\/)\d{2}(\s)\d{2}:\d{2}:\d{2}/i";
        $p2 = "/\d{4}(\-|\.|\/)\d{2}(\-|\.|\/)\d{2}(\s)\d{2}:\d{2}/";
        $matrix = [$p1, $p2];
        foreach ($matrix as $pattern) {
            if (preg_match($pattern, $string)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是日期格式
     * @param $string
     * @return bool
     */
    public static function isDate($string)
    {
        $p1 = "/^\d{4}(\-|\.|\/)\d{2}(\-|\.|\/)\d{2}$/";
        $matrix = [$p1];
        foreach ($matrix as $pattern) {
            if (preg_match($pattern, $string)) {
                return true;
            }
        }
        return false;
    }
}
